package com.kaili.basic.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.kaili.basic.config.FastDFSConfig;
import com.kaili.basic.mapper.BaseDocumentMapper;
import com.kaili.basic.pojo.dto.BaseDocumentDto;
import com.kaili.basic.pojo.entity.BaseDocumentEntity;
import com.kaili.basic.pojo.vo.BaseDocumentVO;
import com.kaili.basic.service.BaseDocumentService;
import com.kaili.common.fastdfs.FastDFSClient;
import com.kaili.common.util.LoginDetailUtils;
import com.kaili.common.util.ObjectUtils;
import org.apache.commons.lang.StringUtils;
import org.csource.common.MyException;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.ProtoCommon;
import org.csource.fastdfs.StorageClient1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.List;

/**
 * @Desc: 文件管理实现类
 * @author: zengshuai
 * @Time: 19-7-23 下午6:06
 * @Copyright: © 2018 杭州凯立通信有限公司 版权所有
 * @Warning: 本内容仅限于公司内部传阅, 禁止外泄或用于其它商业目的
 */
@Service
public class BaseDocumentServiceImpl extends ServiceImpl<BaseDocumentMapper,BaseDocumentEntity> implements BaseDocumentService{

    @Autowired
    private BaseDocumentMapper baseDocumentMapper;

    @Autowired
    private FastDFSConfig dfsConfig;

    @Override
    public void addDocument(BaseDocumentEntity entity) {
        entity.setCreateName(LoginDetailUtils.getUserName());
        baseDocumentMapper.insert(entity);

    }

    @Override
    public Page<BaseDocumentVO> queryPageList(Page<BaseDocumentVO> pager, BaseDocumentDto dto) {
        List<BaseDocumentVO> list = baseMapper.queryPageList(pager, ObjectUtils.objectToMap(dto));
        String filepath = "";
        for (BaseDocumentVO VO: list) {
            if(StringUtils.isNotEmpty(VO.getFileUrl())){
                filepath = VO.getFileUrl().substring(VO.getFileUrl().indexOf("g"));
                VO.setFileUrl("http://"+dfsConfig.getIp()+"/"+filepath);
            }
        }
        return pager.setRecords(list);
    }

    @Override
    public void deleteDocument(Long id) {
        // 1.逻辑删除文件记录信息
        BaseDocumentEntity entity = new BaseDocumentEntity();
        entity.setId(id);
        entity.setRdStatus(0);
        baseDocumentMapper.updateById(entity);
        // 2.删除FastDFS服务器文件
        String fileUrl = baseDocumentMapper.selectById(id).getFileUrl();
        if(fileUrl != null){
            String filepath = fileUrl.substring(fileUrl.indexOf("g"));
            try {
                FastDFSClient.deleteFile(filepath.substring(0,filepath.indexOf("/")),filepath.substring(filepath.indexOf("/")+1));
            } catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    @Override
    public List<BaseDocumentVO> queryExportList(BaseDocumentDto dto) {
        return baseDocumentMapper.queryPageList(ObjectUtils.objectToMap(dto));
    }

}
